import { apiList } from "@/api/apiList";
function useCommon(params) {

  let commonds = []

  let commonlayers = []

  let _imageLayer = ''

  let _layer = new Cesium.CustomDataSource('plotlayer')

  let _srs = 'EPSG:4326'

  function loadTif(item) {

    loadjson(item)

    let rec = getrectangle(item)


    let params = {
      layername: 'taiwan13-1',
      tifpath: 'taiwan13-1.tif',
    }

    

    params.tifpath = item.imgpath
    let namearr = item.imgpath.split("/")
    params.layername = namearr.pop().split(".")[0]
    apiList.shujuguanli.genWmsService(params).then(res => {

      // wmts
      let basemapSource = new Cesium.WebMapTileServiceImageryProvider({
        //  对与url  很多博客都说要进行 如下拼接
        // 后端返回的wmts/rest/{layer}/{style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}?format=image/png
        // 其实不用拼接  只需要后端返回的/wtms路径即可
        url: '/geoserver/gwc/service/wmts', //服务地址 服务地址只需要后端返回的 /WMTS   不需要给后面拼接任何东西，  
        layer: `test_tif:${params.layername}`, //图层名称 : "pucheng:satellite"
        style: '',
        format: 'image/png',
        tileMatrixSetID: _srs,  // "EPSG:900913"
        // 地图飞入  定位的坐标  后端返回的数据
        rectangle: rec,
        // 必须加  不然地图出不来
        //之所以要在这里提出这个tileMatrixLabels参数，是因为GeoServer在缓冲切分瓦片时对每一个缩放级别的存储目录没有使用相应的数字，而使用了网格+级别的方式来命名，如“EPSG:9100913:10”表示的是使用“EPSG:9100913”网格切分的第10级瓦片。
        tileMatrixLabels: [`${_srs}:0`, `${_srs}:1`, `${_srs}:2`, `${_srs}:3`, `${_srs}:4`, `${_srs}:5`, `${_srs}:6`,
        `${_srs}:7`, `${_srs}:8`, `${_srs}:9`, `${_srs}:10`, `${_srs}:11`, `${_srs}:12`, `${_srs}:13`, `${_srs}:14`,
        `${_srs}:15`, `${_srs}:16`, `${_srs}:17`, `${_srs}:18`,
        ],
        // 必须加  不然地图出不来
        tilingScheme: new Cesium.GeographicTilingScheme({//此处很重要，很重要。。。
          numberOfLevelZeroTilesX: 2,
          numberOfLevelZeroTilesY: 1
        }),
        // tilematrixset: _srs,
        // maximumLevel: 18,
      });
      _imageLayer = viewer.imageryLayers.addImageryProvider(basemapSource);

    })

  }

  function getrectangle(item) {

    // 计算外包围盒
    let west = Number.POSITIVE_INFINITY;
    let south = Number.POSITIVE_INFINITY;
    let east = Number.NEGATIVE_INFINITY;
    let north = Number.NEGATIVE_INFINITY;


    item.item.geometry.coordinates[0].map(iitem => {
      west = Math.min(west, Cesium.Math.toRadians(iitem[0]));
      south = Math.min(south, Cesium.Math.toRadians(iitem[1]));
      east = Math.max(east, Cesium.Math.toRadians(iitem[0]));
      north = Math.max(north, Cesium.Math.toRadians(iitem[1]));
    })

    const rectangle = new Cesium.Rectangle(west, south, east, north);

    return rectangle

  }

  function loadjson(params) {
    // loadjson

    Cesium.GeoJsonDataSource.load(params.item, {
      stroke: Cesium.Color.HOTPINK,
      fill: Cesium.Color.fromAlpha(Cesium.Color.RED, 0.0),
      strokeWidth: 3,
    }).then((res) => {
      viewer.dataSources.add(res);
      viewer.flyTo(res,{
        duration: 0.2
      });
      commonds.push(res)
    })


  }


  function removeAll() {

    commonlayers.map(item => {
      viewer.imageryLayers.remove(item)
    })
    commonlayers = []

    commonds.map(item => {
      viewer.dataSources.remove(item);
    })
    commonds = []

    _layer.entities.removeAll()

    if (_imageLayer != '') {
      viewer.imageryLayers.remove(_imageLayer)
      _imageLayer = ''
    }

  }

  return {
    loadTif,
    removeAll,
  }
}

export default useCommon